package com.ojoin.trade.supplyloan_core.web.service.impl;

import com.ojoin.trade.common.exception.BusinessException;
import com.ojoin.trade.common.utils.CommonUtils;
import com.ojoin.trade.common.utils.TimeUtils;
import com.ojoin.trade.common.web.dao.BaseDao;
import com.ojoin.trade.common.web.service.impl.BaseServiceImpl;
import com.ojoin.trade.supplyloan_core.constdef.ISLConstDef;
import com.ojoin.trade.supplyloan_core.web.dao.TbSlLoanRefundDao;
import com.ojoin.trade.supplyloan_core.web.domain.*;
import com.ojoin.trade.supplyloan_core.web.service.TbSlLoanRefundService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 15-10-28
 * Time: 下午1:24
 * To change this template use File | Settings | File Templates.
 */
@Service
public class TbSlLoanRefundServiceImpl extends BaseServiceImpl<TbSlLoanRefundLog> implements TbSlLoanRefundService {

    @Autowired
    private TbSlLoanRefundDao dao;
    @Override
    protected BaseDao<TbSlLoanRefundLog> getBaseDao() {
        return dao;
    }

    @Override
    public void insertTbSlLoanRefund(TbSlLoanRefundLog refundLogvo) throws BusinessException {
        ((TbSlLoanRefundDao)this.getBaseDao()).insertTbSlLoanRefund(refundLogvo);
    }

    @Override
    public List<TbSlLoanRefundLogVo> queryTbSlLoanReFundvos(int Joinid,Pageable page) throws BusinessException {
        return  ((TbSlLoanRefundDao)this.getBaseDao()).queryTbSlLoanReFundvos(Joinid,page) ;
    }

    @Override
    public List<TbSlRepayCreditVo> queryRepayCreditByJoinid(int joinid, Pageable page) throws BusinessException {
        return    ((TbSlLoanRefundDao)this.getBaseDao()).queryRepayCreditvo(joinid,page);
    }

    @Override
	public List<TbSlLoanRefundVo> queryLoanRefundlist(int joinid, String stime,
			String etime, Pageable page) throws BusinessException {
    	 List<TbSlLoanRefundVo> list=((TbSlLoanRefundDao)this.getBaseDao()).queryRefundVo(joinid,stime,etime,page);
    	 return dealRefundList(list);
    }
    
    @Override
    public List<TbSlLoanRefundVo> queryLoanRefundlist(int joinid, Pageable page) throws BusinessException {
        List<TbSlLoanRefundVo> list=((TbSlLoanRefundDao)this.getBaseDao()).queryRefundVo(joinid,page);
        return dealRefundList(list);
    }
    
    private List<TbSlLoanRefundVo> dealRefundList(List<TbSlLoanRefundVo> list) throws BusinessException {
   
        if(!CommonUtils.isEmpty(list)){
         for(TbSlLoanRefundVo vo:list){
             try{
             String refundDate=vo.getRefundDate();
             String lastRefundTime=vo.getLastRefundTime();
             String renewedBeginTime=vo.getRenewedBeginTime();
             String renewedRefundTime=vo.getRenewedRefundTime();
             long shouldRefundPrinciple=CommonUtils.getLong(vo.getShouldRefundPrinciple());
             long shouldRefundRate=CommonUtils.getLong(vo.getShouldRefundRate());
             long refundPrinciple=CommonUtils.getLong(vo.getRefundPrinciple());
             long refundFee=CommonUtils.getLong(vo.getRefundFee());
             long refundRenewedAmount=CommonUtils.getLong(vo.getRefundRenewedAmount());
             long  surplusPrinciple=shouldRefundPrinciple-refundPrinciple;
             long surplusFee=shouldRefundRate-refundFee;
             vo.setSurplusPrinciple(new Long(surplusPrinciple).toString());
             vo.setSurplusFee(new Long(surplusFee).toString());
             vo.setRefundAmount(new Long(refundPrinciple+refundFee+refundRenewedAmount).toString());
             vo.setLoanstatus(ISLConstDef.LOAN_NORMAL);
             if(refundDate.compareTo(lastRefundTime)>0){
                 if(CommonUtils.isEmpty(renewedBeginTime)||CommonUtils.isEmpty(renewedRefundTime)){
                        vo.setLoanstatus(ISLConstDef.LOAN_OVERDUE);
                     BigDecimal normalIntrest=new BigDecimal("0");
                     if(!CommonUtils.isEmpty(vo.getNormalIntrest())){
                        normalIntrest=new BigDecimal(vo.getNormalIntrest()).divide(new BigDecimal(100));
                     }
                     BigDecimal pentyIntrest=new BigDecimal("0");
                     if(!CommonUtils.isEmpty(vo.getPentyIntrest())){
                         pentyIntrest=new BigDecimal(vo.getPentyIntrest()).divide(new BigDecimal(100));
                     }
                     BigDecimal  refundNormalFee=new BigDecimal("0");
                     if(!CommonUtils.isEmpty(vo.getRefundNormalFee())){
                         refundNormalFee=new BigDecimal(vo.getRefundNormalFee()).divide(new BigDecimal(100));
                     }
                     BigDecimal pentyFee=new BigDecimal("0");
                     if(!CommonUtils.isEmpty(vo.getRefundPentyFee())){
                      pentyFee=new BigDecimal(vo.getRefundPentyFee()).divide(new BigDecimal(100));
                     }
                     BigDecimal surplusNormalIntrest=normalIntrest.subtract(refundNormalFee);
                     BigDecimal surplusPentyFee=pentyIntrest.subtract(pentyFee);
                     vo.setShouldRefundRateTips("正常利息:" + normalIntrest.toString() +
                             "元;罚息：" + pentyIntrest.toString() + "元");
                     vo.setRefundRateTips("正常利息:"+refundNormalFee.toString()+"元;罚息："+pentyFee.toString());
                     vo.setSurplusRateTips("正常利息："+surplusNormalIntrest.toString()
                     +"元;罚息："+surplusPentyFee.toString()+"元");
                     long normaldays= TimeUtils.getDateSpaceOfDay(vo.getRealLoanTime(),vo.getLastRefundTime())+1;
                     long pentydays=TimeUtils.getDateSpaceOfDay(vo.getLastRefundTime(),vo.getRefundDate())-1; //最后一天计息为正常利息
                     vo.setCalculateTips("正常计息："+normaldays+"天;"+"罚息："+pentydays+"天");
                     vo.setDays(normaldays+pentydays);
                     BigDecimal renewedamount=new BigDecimal("0");
                     if(!CommonUtils.isEmpty(vo.getRenewedAmount())){
                         renewedamount=new BigDecimal(vo.getRenewedAmount());
                     }
                     BigDecimal refundrenewedamount=new BigDecimal("0");
                     if(!CommonUtils.isEmpty(vo.getRefundRenewedAmount())){
                         refundrenewedamount=new BigDecimal(vo.getRefundRenewedAmount());
                     }
                     BigDecimal surplusrenewedamount=renewedamount.subtract(refundrenewedamount);
                     vo.setSurplusRenewedAmount(surplusrenewedamount.toString());
                      }else{
                      if(vo.getRefundDate().compareTo(renewedRefundTime)<=0){
                          vo.setLoanstatus(ISLConstDef.LOAN_RENEWED);
                          BigDecimal normalIntrest=new BigDecimal("0");
                          if(!CommonUtils.isEmpty(vo.getNormalIntrest())){
                              normalIntrest=new BigDecimal(vo.getNormalIntrest()).divide(new BigDecimal(100));
                          }
                          BigDecimal renewedIntrest=new BigDecimal("0");
                          if(!CommonUtils.isEmpty(vo.getRenewedIntrest())){
                              renewedIntrest=new BigDecimal(vo.getRenewedIntrest()).divide(new BigDecimal(100));
                          }
                          BigDecimal  refundNormalFee=new BigDecimal("0");
                          if(!CommonUtils.isEmpty(vo.getRefundNormalFee())){
                              refundNormalFee=new BigDecimal(vo.getRefundNormalFee()).divide(new BigDecimal(100));
                          }
                          BigDecimal  refundRenewFee=new BigDecimal("0");
                          if(!CommonUtils.isEmpty(vo.getRefundRenewedFee())){
                              refundRenewFee=new BigDecimal(vo.getRefundRenewedFee()).divide(new BigDecimal(100));
                          }
                          BigDecimal surplusNormalIntrest=normalIntrest.subtract(refundNormalFee);
                          BigDecimal surplusRenewedIntrest=renewedIntrest.subtract(refundRenewFee);
                          vo.setShouldRefundRateTips("正常利息：" + normalIntrest.toString() + "元;展期利息：" + renewedIntrest.toString() +
                                  "元");
                          vo.setRefundRateTips("正常利息："+refundNormalFee.toString()+"元;展期利息："+refundRenewFee.toString()+"元;");
                          vo.setSurplusRateTips("正常利息："+surplusNormalIntrest.toString()+"元;展期利息："+surplusRenewedIntrest.toString()
                                  +"元;");
                          long normaldays= TimeUtils.getDateSpaceOfDay(vo.getRealLoanTime(),vo.getLastRefundTime())+1;
                          long reneweddays=TimeUtils.getDateSpaceOfDay(vo.getRenewedBeginTime(),vo.getRefundDate());
                          vo.setCalculateTips("正常计息："+normaldays+"天;"+"展期："+reneweddays+"天");
                          vo.setDays(normaldays+reneweddays);
                          BigDecimal renewedamount=new BigDecimal("0");
                          if(!CommonUtils.isEmpty(vo.getRenewedAmount())){
                              renewedamount=new BigDecimal(vo.getRenewedAmount());
                          }
                          BigDecimal refundrenewedamount=new BigDecimal("0");
                          if(!CommonUtils.isEmpty(vo.getRefundRenewedAmount())){
                              refundrenewedamount=new BigDecimal(vo.getRefundRenewedAmount());
                          }
                          BigDecimal surplusrenewedamount=renewedamount.subtract(refundrenewedamount);
                          vo.setSurplusRenewedAmount(surplusrenewedamount.toString());
                      }else{
                          vo.setLoanstatus(ISLConstDef.LOAN_OVERDUE);
                          BigDecimal normalIntrest=new BigDecimal("0");
                          if(!CommonUtils.isEmpty(vo.getNormalIntrest())){
                              normalIntrest=new BigDecimal(vo.getNormalIntrest()).divide(new BigDecimal(100));
                          }
                          BigDecimal renewedIntrest=new BigDecimal("0");
                          if(!CommonUtils.isEmpty(vo.getRenewedIntrest())){
                              renewedIntrest=new BigDecimal(vo.getRenewedIntrest()).divide(new BigDecimal(100));
                          }
                          BigDecimal pentyIntrest=new BigDecimal("0");
                          if(!CommonUtils.isEmpty(vo.getPentyIntrest())){
                              pentyIntrest=new BigDecimal(vo.getPentyIntrest()).divide(new BigDecimal(100));
                          }
                          BigDecimal  refundNormalFee=new BigDecimal("0");
                          if(!CommonUtils.isEmpty(vo.getRefundNormalFee())){
                              refundNormalFee=new BigDecimal(vo.getRefundNormalFee()).divide(new BigDecimal(100));
                          }
                          BigDecimal refundRenewFee=new BigDecimal("0");
                          if(!CommonUtils.isEmpty(vo.getRefundRenewedFee())){
                              refundRenewFee=new BigDecimal(vo.getRefundRenewedFee()).divide(new BigDecimal(100));
                          }
                          BigDecimal pentyFee=new BigDecimal("0");
                          if(!CommonUtils.isEmpty(vo.getRefundPentyFee())){
                          pentyFee=new BigDecimal(vo.getRefundPentyFee()).divide(new BigDecimal(100));
                          }
                          BigDecimal surplusNormalIntrest=normalIntrest.subtract(refundNormalFee);
                          BigDecimal surplusRenewedIntrest=renewedIntrest.subtract(refundRenewFee);
                          BigDecimal surplusPentyFee=pentyIntrest.subtract(pentyFee);
                          vo.setShouldRefundRateTips("正常利息：" + normalIntrest.toString() + "元;展期利息：" + renewedIntrest.toString() +
                                  "元;罚息：" + pentyIntrest.toString() + "元");
                          vo.setRefundRateTips("正常利息："+refundNormalFee.toString()+"元;展期利息："+refundRenewFee.toString()+"元;罚息："+pentyFee.toString()+"元");
                          vo.setSurplusRateTips("正常利息："+surplusNormalIntrest.toString()+"元;展期利息："+surplusRenewedIntrest.toString()
                                  +"元;罚息："+surplusPentyFee.toString()+"元");
                          long normaldays= TimeUtils.getDateSpaceOfDay(vo.getRealLoanTime(),vo.getLastRefundTime())+1;
                          long pentydays=TimeUtils.getDateSpaceOfDay(vo.getRenewedRefundTime(),vo.getRefundDate())-1;
                          long reneweddays=TimeUtils.getDateSpaceOfDay(vo.getRenewedBeginTime(),vo.getRenewedRefundTime())+1;
                          vo.setCalculateTips("正常计息："+normaldays+"天;展期："+reneweddays+"天;罚息："+pentydays+"天");
                          vo.setDays(normaldays+pentydays+reneweddays);
                          BigDecimal renewedamount=new BigDecimal("0");
                          if(!CommonUtils.isEmpty(vo.getRenewedAmount())){
                              renewedamount=new BigDecimal(vo.getRenewedAmount());
                          }
                          BigDecimal refundrenewedamount=new BigDecimal("0");
                          if(!CommonUtils.isEmpty(vo.getRefundRenewedAmount())){
                              refundrenewedamount=new BigDecimal(vo.getRefundRenewedAmount());
                          }
                          BigDecimal surplusrenewedamount=renewedamount.subtract(refundrenewedamount);
                          vo.setSurplusRenewedAmount(surplusrenewedamount.toString());

                      }



                 }


             }
         }catch (Exception e){
                 throw new BusinessException("操作失败",e);
             }
         }
        }
        return list;
    }

    @Override
    public Long queryTotalInterest(int idSlLoanCreditLog) throws BusinessException {

        return  ((TbSlLoanRefundDao)this.getBaseDao()).queryTotalInterest(idSlLoanCreditLog);
    }

    @Override
    public List<TbSlLoanRefundTotalVo> queryRefundTotalVo(int userIdOfJoinOfDebtor,Pageable page) throws BusinessException {
        List<TbSlLoanRefundTotalVo> list=((TbSlLoanRefundDao)this.getBaseDao()).queryRefundTotalVo(userIdOfJoinOfDebtor,page);
        if(!CommonUtils.isEmpty(list)){
            for(TbSlLoanRefundTotalVo vo:list){
                long shouldRefundPrinciple=CommonUtils.getLong(vo.getShouldRefundPrinciple());
                long shouldRefundRate=CommonUtils.getLong(vo.getShouldRefundRate());
                long refundPrinciple=CommonUtils.getLong(vo.getRefundPrinciple());
                long refundFee=CommonUtils.getLong(vo.getRefundFee());
                long  surplusPrinciple=shouldRefundPrinciple-refundPrinciple;
                long surplusFee=shouldRefundRate-refundFee;
                vo.setSurplusPrinciple(new Long(surplusPrinciple).toString());
                vo.setSurplusFee(new Long(surplusFee).toString());
                long totalPrinciple=CommonUtils.getLong(vo.getAccountDelta())-surplusPrinciple;
                vo.setTotalPrinciple(new Long(totalPrinciple).toString());
            }
        }
        return list;

    }

    @Override
    public List<TbSlCheckAccVo> queryCheckAccVo(String curdate, Pageable page) throws BusinessException {
        return  ((TbSlLoanRefundDao)this.getBaseDao()).queryCheckAccVo(curdate,page);
    }

    @Override
    public List<TbSlLoanRefundVo> queryRenewedRefundForExcel(int joinid) throws BusinessException {
        List<TbSlLoanRefundVo> list=  ((TbSlLoanRefundDao)this.getBaseDao()).queryRenewedRefundForExcel(joinid);
        if(!CommonUtils.isEmpty(list)){
            for(TbSlLoanRefundVo vo:list){
                try{
             String normalIntrest=vo.getNormalIntrest();
             String refundNormalfee=vo.getRefundNormalFee();
             BigDecimal surplusNormalFee=new BigDecimal(normalIntrest).subtract(new BigDecimal(refundNormalfee));
             BigDecimal surplusRenewedFee=new BigDecimal(vo.getRenewedIntrest()).subtract(new BigDecimal(vo.getRefundRenewedFee()));
             BigDecimal surplusAmount=new BigDecimal(vo.getRenewedAmount()).subtract(new BigDecimal(vo.getRefundRenewedAmount()));
            BigDecimal surplusPricipal=new BigDecimal(vo.getShouldRefundPrinciple()).subtract(new BigDecimal(vo.getRefundPrinciple()));
             vo.setSurplusFee(surplusNormalFee.toString());
             vo.setSurplusRenewedAmount(surplusAmount.toString());
             vo.setSurplusRenewedFee(surplusRenewedFee.toString());
             vo.setSurplusPrinciple(surplusPricipal.toString());
             String lastRefundTime=vo.getLastRefundTime();
             String realLoanTime=vo.getRealLoanTime();
             String renewedBegintime=vo.getRenewedBeginTime();
             String renewedEndTime=vo.getRenewedRefundTime();
             Long baseCalculate=TimeUtils.getDateSpaceOfDay(realLoanTime,lastRefundTime)+1;
             String renewedcalculatedate=vo.getRefundDate();
               if(vo.getRefundDate().compareTo(renewedEndTime)>0){
                   renewedcalculatedate=renewedEndTime;
               }
             Long renewedCalculate=TimeUtils.getDateSpaceOfDay(renewedBegintime,renewedcalculatedate);
                    if(vo.getRefundDate().compareTo(renewedEndTime)>0){
                        renewedCalculate=renewedCalculate+1;
                    }
              vo.setBaseCalculateDays(baseCalculate);
              vo.setRenewedCalculateDays(renewedCalculate);
              
              vo.setAccountDelta(CommonUtils.getFenByString(vo.getAccountDelta()));
              vo.setNormalIntrest(CommonUtils.getFenByString(vo.getNormalIntrest()));
              vo.setPentyIntrest(CommonUtils.getFenByString(vo.getPentyIntrest()));
              vo.setRefundAmount(CommonUtils.getFenByString(vo.getRefundAmount()));
              vo.setRefundFee(CommonUtils.getFenByString(vo.getRefundFee()));
              vo.setRefundNormalFee(CommonUtils.getFenByString(vo.getRefundNormalFee()));
              vo.setRefundPentyFee(CommonUtils.getFenByString(vo.getRefundPentyFee()));
              vo.setRefundPrinciple(CommonUtils.getFenByString(vo.getRefundPrinciple()));
              vo.setRefundRenewedAmount(CommonUtils.getFenByString(vo.getRefundRenewedAmount()));
              vo.setRefundRenewedFee(CommonUtils.getFenByString(vo.getRefundRenewedFee()));
              vo.setRenewedAmount(CommonUtils.getFenByString(vo.getRenewedAmount()));
              vo.setRenewedIntrest(CommonUtils.getFenByString(vo.getRenewedIntrest()));
              
              vo.setShouldRefundPrinciple(CommonUtils.getFenByString(vo.getShouldRefundPrinciple()));
              vo.setSurplusPrinciple(CommonUtils.getFenByString(vo.getSurplusPrinciple()));
              vo.setSurplusFee(CommonUtils.getFenByString(vo.getSurplusFee()));
              vo.setSurplusRenewedFee(CommonUtils.getFenByString(vo.getSurplusRenewedFee()));
              vo.setSurplusRenewedAmount(CommonUtils.getFenByString(vo.getSurplusRenewedAmount()));
                            
                }catch (Exception e){
                    throw new BusinessException(e);
                }
            }
        }
        return list;
    }

    @Override
    public List<SLRefundBaseInfoVo> queryRefundBillVo(String begintime, String endtime, Integer userIdOfJoinOfDebtor,Integer userIdOfJoinOfCreditor, Pageable pageable) throws BusinessException {

            List<SLRefundBaseInfoVo> slRefundBaseInfoVoList= ((TbSlLoanRefundDao)this.getBaseDao()).queryRefundBaseInfoVO(begintime, endtime, userIdOfJoinOfDebtor, userIdOfJoinOfCreditor,pageable);
           if(!CommonUtils.isEmpty(slRefundBaseInfoVoList)){
               for(SLRefundBaseInfoVo vo:slRefundBaseInfoVoList) {
                   List<SLRefundDetailVo> slRefundDetailVoList=((TbSlLoanRefundDao)this.getBaseDao()).queryRefundDetailVo(begintime, endtime, vo.getUserIdOfJoinOfDebtor(),userIdOfJoinOfCreditor);
                   vo.setRefundDetailVoList(slRefundDetailVoList);
               }
           }
        return slRefundBaseInfoVoList;
    }


    @Override
    public SLRefundBillVo querySlRefundBillSumvo(String begintime, String endtime, Integer userIdOfJoinOfDebtor, Integer userIdOfJoinOfCreditor) throws BusinessException {
        return  ((TbSlLoanRefundDao)this.getBaseDao()).queryRefundBillVo(begintime, endtime, userIdOfJoinOfDebtor, userIdOfJoinOfCreditor);
    }

}